var homeUrl = "http://doodle4googleau.googlecode.com/svn/trunk/mapplet/";
var imageUrl = "http://mw1.google.com/mw-photos/doodle4google/400x300/";
var imgLeftArrow = homeUrl + "left-arrow.png";
var imgRightArrow = homeUrl + "right-arrow.png";
var logoUrl = homeUrl + "brushes.jpg";

var d4gp = "";
var browserSize = [];
var imageSize = [300,225];

function D4GPlayer(sid, sc, did, sn, yl) {
	this.schoolId = unescape(sid);
	this.schoolName = unescape(sc);
	this.doodleIdList = unescape(did).split(",");
	this.studentList = unescape(sn).split(",");
	this.yearLevelList = unescape(yl).split(",");
	this.currentIndex = 0;
	this.titleDiv = 'titlebar';
	this.slideshowDiv = 'slideshow';
	this.navbarDiv = 'navbar';
	
	this.prevImage = function() {
		if((this.currentIndex = this.currentIndex - 1) < 0)
			this.currentIndex = this.doodleIdList.length - 1;
		this.loadImage(this.currentIndex);
	}
	
	this.nextImage = function() {
		if((this.currentIndex = 1 + this.currentIndex) == this.doodleIdList.length)
			this.currentIndex = 0;
		this.loadImage(this.currentIndex);		
	}
	
	this.loadImage = function (imageIdx) {
		var imageSrc = imageUrl + this.schoolId + '_' + this.doodleIdList[imageIdx] + '.jpg';
		var titleDiv = document.getElementById(this.titleDiv);
		var html = '<table width=\'100%\' border=0 cellspacing=0 cellpadding=0><tr><td align=\'left\'><h3>' + this.schoolName + '</h3><h4>' + this.studentList[imageIdx];
		if (this.yearLevelList[imageIdx] != "") 
			html += ' <i>(Year: ' + this.yearLevelList[imageIdx] + ')</i></td></h4>';
		else
			html += '</td></h4>';		
		html += '<td align=\'right\'><img id=\'d4glogo\' src=\'' + logoUrl +'\' /></td></tr></table>';
		titleDiv.innerHTML = html;
		var imageDiv = document.getElementById(this.slideshowDiv);
		imageDiv.style.display = "block";
		imageDiv.innerHTML = '<center><img id=\'doodleImg\' src=\'' + imageSrc + '\' width=' + imageSize[0] + ' height=' + imageSize[1] + '></img></center>';
		this.writeSelectingArrows(imageIdx, this.doodleIdList.length);
	}
	
	this.writeSelectingArrows = function (imageIdx, imageTotal) {
		prevImageFunc = "d4gp.prevImage()";
		nextImageFunc = "d4gp.nextImage()";
		var out = "<center><table border=0 cellspacing=0 cellpadding=0><tr>";
		out += "<td valign=middle width=16><a href='javascript:void(0)' onClick='"+ prevImageFunc  +"'>";
		out += "<img id=leftArrowImg width=16 height=16 src='"+ imgLeftArrow +"' border=0></img></a></td>";
		out += "<td valign=middle width=40 align=center><span style='font-size:9px'> "+(1 + imageIdx)+" of "+ imageTotal  +" </span></td>";
		out += "<td valign=middle width=16><a href='javascript:void(0)' onClick='"+ nextImageFunc  +"'>";
		out += "<img id=rightArrowImg height=16 width=16 src='"+ imgRightArrow +"' border=0></img></a></td>";
		out += "</tr></table></center>";
		
		var arrowDiv = document.getElementById(this.navbarDiv);
		arrowDiv.style.display = "block";
		arrowDiv.innerHTML = out;		
	}	
}

function gup( name ) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}


function init() {
	var did = gup('did');
	var sn = gup('sn');
	var yl = gup('yl');
	var re = /,$/;
	did = did.replace(re, "");
	sn = sn.replace(re, "");
	yl = yl.replace(re, "");
	//d4gp = new D4GPlayer(gup('sid'), gup('sc'), gup('did'), gup('sn'), gup('yl'));
	d4gp = new D4GPlayer(gup('sid'), gup('sc'), did, sn, yl);
	d4gp.loadImage(0);
}
